import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'
Vue.use(Vuex)
Vue.use(axios)
export default new Vuex.Store({
    state: {

        //定义数组存储读取到的json文件
        carousel: [], //刘
        newAlbums: [],
        playName: '',
        playSingers: [],
        playUrl: '',
        playImg: "http://p2.music.126.net/rAK40e1i4vf59OnTac8gHQ==/109951164948474498.jpg?param=140y140",
        songs: []
    },
    mutations: {
        //刘
        setInfo(state, info) {
            state.carousel = info;
        },
        getNewAlnum(state) {
            axios
                .get("https://autumnfish.cn/album/newest")
                .then(newAlbum => {
                    state.newAlbums = newAlbum.data.albums.filter((ele, index) => {
                        return index < 10;
                    });
                })
                .catch(err => {
                    console.log(err);
                });
        },
        chang(state, id) { //(播放歌曲调的方法传递参数为数组[歌曲id，专辑id,歌曲名,歌手数组[]])
            //state.playSong = playSong;
            //console.log(state.playSong);
            // axios.get("https://api.imjad.cn/cloudmusic/?id=" + songId).then(playSong => {
            // state.playName = playinfo[2]
            // state.playSingers = playinfo[3]

            state.playSingers = []
            axios.get("https://autumnfish.cn/song/url?id=" + id).then(song => {
                state.playUrl = song.data.data[0].url
            }).catch(err => {
                console.log(err);
            })
            axios.get('https://autumnfish.cn/song/detail?ids=' + id).then(alb => {
                let songInfo = alb.data.songs[0]

                state.playName = songInfo.name
                    // state.playImg=songInfo
                songInfo.ar.forEach(ele => {
                    state.playSingers.push(ele.name)
                });
                state.playImg = songInfo.al.picUrl
                    // console.log(state.playImg);
            }).catch(err => {
                console.log(err);
            })
        },
        changeSongs(state, songs) {
            state.songs = songs
        },

    },
    actions: {
        // 通过axios获取本地的json文件数据 刘
        init(contest) {
            //通过axios的get方式获取public下的json里的json文件
            axios.get("../json/carousel.json").then(input => {
                contest.commit("setInfo", input.data);
            }).catch(err => {
                console.log(err);
            })

        }
    },
    modules: {},
    getters: {
        changeMusicTime: function() {

            return (ti) => {
                var min = parseInt(ti / (1000 * 60));
                var sec = parseInt((ti % (1000 * 60)) / 1000);
                if (min < 10) {
                    min = "0" + min;
                }
                if (sec < 10) {
                    sec = "0" + sec;
                }
                return min + ":" + sec;
            }

        }
    }
})